Tutorial > Excel VBA

Excel VBA

Communicate with the PIA4800 series using VISA through GPIB, RS232C or USB.

At first, display the tool bar for "Visual Basic" and "Control Tool Box" on the screen.

Activating the "Visual Basic Editor"

After displaying the tool bars, activate the "Visual Basic Editor".

Select [Tool] > [Macro] > [Visual Basic Editor]

Setting the communication driver.

After activation of the "Visual Basic Editor", set the communication driver (VISA library).

Click "Tool" on the menu bar, then select "Add Reference" to refer "VISA COM 3.0 Type Library".

Programming

After setting the communication driver, it enables to communicate by GPIB, RS232C or USB.

Set to the "Design" mode.

Choose button in the "Control Tool Box", then click on the Excel sheet.

Double click on the button specified on the sheet.

Write the program here.

The following describes the communication procedure for via VISA.

Open the VISA

To communicate with the device of GPIB, RS232C or USB via VISA, it requires to open the VISA first. When opening the VISA, specify the I/O resource.

Example : To open VISA by using USB

Set rm = CreateObject("VISA.GlobalRM")

Set msg = rm.Open("USB::0x0B3E::0x1014::00000001::INSTR", NO_LOCK, 0, "")

"USB::0x0B3E::0x1014::00000001::INSTR" is the I/O resource.

The I/O resource is specified by the following constructions. The part indicated with [ ] can be abbreviated. Enter the appropriate value in the part specified in oblique characters.

GPIB | GPIB[board]::PrimaryAddress[::SecondaryAddress][::INSTR] Example : The primary address 3 of the measurering instrument connected to GPIB0. GPIB0::3::INSTR
Serial (RS232C) | ASRL[board][::INSTR] Example : The measurering instrument connected to the serial port COM1. ASRL1::INSTR
USB | USB[board]::VendorID::ProductID::SerialNumber[::InterfaceNumber][::INSTR] Example : The USNTMC measurering instrument having with the vendor ID (VID)2878, Product ID(PID)4116 and serial number "00000001". USB0::0x0B3E::0x1014::00000001::INSTR

For VISA, the alias can be used for the I/O resource.

When using the alias for the I/O resource, even the alias name to be hard coating directly in the application, it can be easily converted to the appropriate I/O resource name.

Example : When using the alias (MYPIA) for the I/O resource.

Set msg = rm.Open("MYPIA", NO_LOCK, 0, "")

When the alias is used, the actual I/O resource is specified by such an external configuration table.

When using the USB (example for KI-VISA)

In case of using VISA other than KI-VISA, please refer to the applied VISA manual.

Controlling the devices

Next, using such a "Read", "Write" to control the devices.

Example:

msg.WriteString ("NODE 5") 'Specify a node address.
msg.WriteString ("CH 1") 'Specify Channel 1.
msg.WriteString ("VSET 10.0") 'Set 10V.
msg.WriteString ("ISET 1.0") 'Set 1A.
msg.WriteString ("OUT 1") 'Output ON.

Closing the VISA.

Close the VISA at the end.

msg.Close

Sample program for controlling a power supply by the PIA4850

Option Explicit

Dim rm As VisaComLib.ResourceManager

Dim msg As VisaComLib.IMessage

Private Sub CommandButton1_Click()

Set rm = CreateObject("VISA.GlobalRM") 'Open the VISA

Set msg = rm.Open("USB::0x0B3E::0x1014::00000001::INSTR", NO_LOCK, 0, "")

msg.WriteString ("TRM 2") 'Set the terminator to EOI.

msg.WriteString ("NODE 5") 'Specify a node address.

msg.WriteString ("CH 1") 'Specify Channel 1.

msg.WriteString ("VSET 10") 'Set 10V.

msg.WriteString ("ISET 1.0") 'Set 1A.

msg.WriteString ("OUT 1") 'Turn output ON.

msg.WriteString ("VOUT?") 'Specify a measured voltage value.

Cells(1, 1) = msg.ReadString(20)

msg.Close 'Close the VISA

End Sub